 package client.inventory;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.awt.Point;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleItemInformationProvider;
 import server.movement.LifeMovement;
 import server.movement.LifeMovementFragment;

 public class MaplePet
   implements Serializable
 {
/*  22 */   private static final Logger log = LogManager.getLogger(MaplePet.class);
   private static final long serialVersionUID = 9179541993413738569L;
/*  24 */   private final int[] excluded = new int[10];
   private String name;
/*  26 */   private int Fh = 0; private int uniqueid; private int stance = 0; private int petitemid; private int secondsLeft = 0; private int skillid; private int color = -1; private int addSkill = 0;
   private Point pos;
/*  28 */   private byte fullness = 100; private byte level = 1; private byte summoned = 0;
/*  29 */   private short inventorypos = 0; private short closeness = 0; private short flags = 0;

   private boolean changed = false;

   private boolean canPickup = true;

   public MaplePet() {}

   private MaplePet(int petitemid, int uniqueid) {
/*  38 */     this.petitemid = petitemid;
/*  39 */     this.uniqueid = uniqueid;
/*  40 */     for (int i = 0; i < this.excluded.length; i++) {
/*  41 */       this.excluded[i] = 0;
     }
   }

   private MaplePet(int petitemid, int uniqueid, short inventorypos) {
/*  46 */     this.petitemid = petitemid;
/*  47 */     this.uniqueid = uniqueid;
/*  48 */     this.inventorypos = inventorypos;
/*  49 */     for (int i = 0; i < this.excluded.length; i++) {
/*  50 */       this.excluded[i] = 0;
     }
   }

   public static MaplePet loadFromDb(int itemid, int petid, short inventorypos) {
/*  55 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  56 */       MaplePet ret = new MaplePet(itemid, petid, inventorypos);
/*  57 */       try (PreparedStatement ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?")) {
/*  58 */         ps.setInt(1, petid);
/*  59 */         try (ResultSet rs = ps.executeQuery()) {
/*  60 */           if (!rs.next()) {
/*  61 */             rs.close();
/*  62 */             ps.close();
/*  63 */             return null;
           }
/*  65 */           ret.setName(rs.getString("name"));
/*  66 */           ret.setCloseness(rs.getShort("closeness"));
/*  67 */           ret.setLevel(rs.getByte("level"));
/*  68 */           ret.setFullness(rs.getByte("fullness"));
/*  69 */           ret.setSecondsLeft(rs.getInt("seconds"));
/*  70 */           ret.setFlags(rs.getShort("flags"));
/*  71 */           ret.setBuffSkill(rs.getInt("skillid"));
/*  72 */           ret.setColor(rs.getInt("color"));
/*  73 */           ret.setAddSkill(rs.getInt("addSkill"));
/*  74 */           String[] list = rs.getString("excluded").split(",");
/*  75 */           for (int i = 0; i < ret.excluded.length; i++) {
/*  76 */             ret.excluded[i] = Integer.parseInt(list[i]);
           }
/*  78 */           ret.changed = false;
         }
       }
/*  81 */       return ret;
/*  82 */     } catch (SQLException ex) {
/*  83 */       log.error("加载宠物信息出错", ex);
/*  84 */       return null;
     }
   }




   public static MaplePet createPet(int itemid, int uniqueid) {
/*  92 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  93 */     return createPet(itemid, ii.getName(itemid), 1, 0, 100, uniqueid, (itemid == 5000054) ? 18000 : 0, ii.getPetFlagInfo(itemid), 0, -1);
   }

   public static MaplePet createPet(int itemid, String name, int level, int closeness, int fullness, int uniqueid, int secondsLeft, short flag, int skillId, int color) {
/*  97 */     if (uniqueid <= -1) {
/*  98 */       uniqueid = MapleInventoryIdentifier.getInstance();
     }
/* 100 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 101 */         PreparedStatement pse = con.prepareStatement("INSERT INTO pets (petid, name, level, closeness, fullness, seconds, flags, skillid, color) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
/* 102 */       pse.setInt(1, uniqueid);
/* 103 */       pse.setString(2, name);
/* 104 */       pse.setByte(3, (byte)level);
/* 105 */       pse.setShort(4, (short)closeness);
/* 106 */       pse.setByte(5, (byte)fullness);
/* 107 */       pse.setInt(6, secondsLeft);
/* 108 */       pse.setShort(7, flag);
/* 109 */       pse.setInt(8, skillId);
/* 110 */       pse.setInt(9, color);
/* 111 */       pse.executeUpdate();
     }
/* 113 */     catch (SQLException ex) {
/* 114 */       log.error("创建宠物信息出错", ex);
/* 115 */       return null;
     }
/* 117 */     MaplePet pet = new MaplePet(itemid, uniqueid);
/* 118 */     pet.setName(name);
/* 119 */     pet.setLevel(level);
/* 120 */     pet.setFullness(fullness);
/* 121 */     pet.setCloseness(closeness);
/* 122 */     pet.setFlags(flag);
/* 123 */     pet.setSecondsLeft(secondsLeft);
/* 124 */     pet.setBuffSkill(skillId);

/* 126 */     return pet;
   }

   public void saveToDb() {
/* 130 */     if (!this.changed) {
       return;
     }
/* 133 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 134 */       try (PreparedStatement ps = con.prepareStatement("UPDATE pets SET name = ?, level = ?, closeness = ?, fullness = ?, seconds = ?, flags = ?, skillid = ?, excluded = ?, color = ?, addSkill = ? WHERE petid = ?")) {
/* 135 */         ps.setString(1, this.name);
/* 136 */         ps.setByte(2, this.level);
/* 137 */         ps.setShort(3, this.closeness);
/* 138 */         ps.setByte(4, this.fullness);
/* 139 */         ps.setInt(5, this.secondsLeft);
/* 140 */         ps.setShort(6, this.flags);
/* 141 */         ps.setInt(7, this.skillid);
/* 142 */         StringBuilder list = new StringBuilder();
/* 143 */         for (int anExcluded : this.excluded) {
/* 144 */           list.append(anExcluded);
/* 145 */           list.append(",");
         }
/* 147 */         String newlist = list.toString();
/* 148 */         ps.setString(8, newlist.substring(0, newlist.length() - 1));
/* 149 */         ps.setInt(9, this.color);
/* 150 */         ps.setInt(10, this.addSkill);
/* 151 */         ps.setInt(11, this.uniqueid);
/* 152 */         ps.executeUpdate();
       }
/* 154 */       this.changed = false;
/* 155 */     } catch (SQLException ex) {
/* 156 */       log.error("保存宠物信息出错", ex);
     }
   }

   public String getName() {
/* 161 */     return this.name;
   }

   public void setName(String name) {
/* 165 */     this.name = name;
/* 166 */     this.changed = true;
   }

   public boolean getSummoned() {
/* 170 */     return (this.summoned > 0);
   }

   public void setSummoned(int summoned) {
/* 174 */     this.summoned = (byte)summoned;
   }

   public byte getSummonedValue() {
/* 178 */     return this.summoned;
   }

   public short getInventoryPosition() {
/* 182 */     return this.inventorypos;
   }

   public void setInventoryPosition(short inventorypos) {
/* 186 */     this.inventorypos = inventorypos;
   }

   public int getUniqueId() {
/* 190 */     return this.uniqueid;
   }

   public short getCloseness() {
/* 194 */     return this.closeness;
   }

   public void setCloseness(int closeness) {
/* 198 */     this.closeness = (short)closeness;
/* 199 */     this.changed = true;
   }

   public byte getLevel() {
/* 203 */     return this.level;
   }

   public void setLevel(int level) {
/* 207 */     this.level = (byte)level;
/* 208 */     this.changed = true;
   }

   public byte getFullness() {
/* 212 */     return this.fullness;
   }

   public void setFullness(int fullness) {
/* 216 */     this.fullness = (byte)fullness;
/* 217 */     this.changed = true;
   }

   public short getFlags() {
/* 221 */     return this.flags;
   }

   public void setFlags(int fffh) {
/* 225 */     this.flags = (short)fffh;
/* 226 */     this.changed = true;
   }

   public int getFh() {
/* 230 */     return this.Fh;
   }

   public void setFh(int Fh) {
/* 234 */     this.Fh = Fh;
   }

   public Point getPos() {
/* 238 */     return this.pos;
   }

   public void setPos(Point pos) {
/* 242 */     this.pos = pos;
   }

   public byte getType() {
/* 246 */     return 3;
   }

   public int getStance() {
/* 250 */     return this.stance;
   }

   public void setStance(int stance) {
/* 254 */     this.stance = stance;
   }

   public int getPetItemId() {
/* 258 */     return this.petitemid;
   }

   public boolean canConsume(int itemId) {
/* 262 */     MapleItemInformationProvider mii = MapleItemInformationProvider.getInstance();
/* 263 */     for (Iterator<Integer> iterator = mii.getItemEffect(itemId).getPetsCanConsume().iterator(); iterator.hasNext(); ) { int petId = ((Integer)iterator.next()).intValue();
/* 264 */       if (petId == this.petitemid) {
/* 265 */         return true;
       } }

/* 268 */     return false;
   }

   public void updatePosition(List<LifeMovementFragment> movement) {
/* 272 */     for (LifeMovementFragment move : movement) {
/* 273 */       if (move instanceof LifeMovement) {
/* 274 */         if (move instanceof server.movement.AbsoluteLifeMovement) {
/* 275 */           setPos(move.getPosition());
         }
/* 277 */         setStance(((LifeMovement)move).getNewstate());
       }
     }
   }

   public int getSecondsLeft() {
/* 283 */     return this.secondsLeft;
   }

   public void setSecondsLeft(int sl) {
/* 287 */     this.secondsLeft = sl;
/* 288 */     this.changed = true;
   }

   public int getBuffSkill() {
/* 292 */     return this.skillid;
   }

   public void setBuffSkill(int id) {
/* 296 */     this.skillid = id;
/* 297 */     this.changed = true;
   }

   public int getColor() {
/* 301 */     return this.color;
   }

   public void setColor(int color) {
/* 305 */     this.color = color;
/* 306 */     this.changed = true;
   }

   public int getAddSkill() {
/* 310 */     return this.addSkill;
   }

   public void setAddSkill(int addSkill) {
/* 314 */     this.addSkill = addSkill;
   }




   public void clearExcluded() {
/* 321 */     for (int i = 0; i < this.excluded.length; i++) {
/* 322 */       this.excluded[i] = 0;
     }
/* 324 */     this.changed = true;
   }

   public List<Integer> getExcluded() {
/* 328 */     List<Integer> list = new ArrayList<>();
/* 329 */     for (int anExcluded : this.excluded) {
/* 330 */       if (anExcluded > 0 && PetFlag.PET_IGNORE_PICKUP.check(this.flags)) {
/* 331 */         list.add(Integer.valueOf(anExcluded));
       }
     }
/* 334 */     return list;
   }

   public void addExcluded(int i, int itemId) {
/* 338 */     if (i < this.excluded.length) {
/* 339 */       this.excluded[i] = itemId;
/* 340 */       this.changed = true;
     }
   }




   public boolean isCanPickup() {
/* 348 */     return this.canPickup;
   }

   public void setCanPickup(boolean can) {
/* 352 */     this.canPickup = can;
   }
 }


/* Location:              C:\PGX\Server.jar!\client\inventory\MaplePet.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */